package cn.signalpha.modules.com.service;

import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.com.entity.UserWallet;
import cn.signalpha.modules.com.entity.enums.WalletAmountTypeEnum;
import cn.signalpha.modules.com.excel.UserWalletExcel;
import cn.signalpha.modules.com.mapper.UserWalletMapper;
import cn.signalpha.modules.com.vo.UserWalletVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class UserWalletService extends ServiceImpl<UserWalletMapper, UserWallet> {

	/**
	 * 自定义分页
	 */
	public IPage<UserWalletVO> selectUserWalletPage(IPage<UserWalletVO> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.selectUserWalletPage(page, params));
	}

	/**
	 * 自定义导出
	 */
	public List<UserWalletExcel> selectUserWalletExcel(Map<String, Object> params) {
		return BeanUtil.copy(baseMapper.selectUserWalletPage(null, params), UserWalletExcel.class);
	}

	/**
	 * 自定义Wrapper
	 */
	public QueryWrapper<UserWallet> selectUserWalletWrapper(Map<String, Object> params) {
		QueryWrapper<UserWallet> wrapper = Wrappers.query();
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("userId")), UserWallet::getUserId, params.get("userId"));
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("type")), UserWallet::getType, params.get("type"));
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("amountType")), UserWallet::getAmountType, params.get("amountType"));
		wrapper.lambda().in(ObjectUtil.isNotEmpty(params.get("sourceType")), UserWallet::getSourceType, Func.toStr(String.valueOf(params.get("sourceType"))));
		wrapper.lambda().ge(ObjectUtil.isNotEmpty(params.get("createTimeBegin")), UserWallet::getCreateTime, params.get("createTimeBegin"));
		wrapper.lambda().le(ObjectUtil.isNotEmpty(params.get("createTimeEnd")), UserWallet::getCreateTime, params.get("createTimeEnd"));
		return wrapper;
	}

	/**
	 * 统计金额
	 */
	public Double sumByParams(Map<String, Object> params) {
		QueryWrapper<UserWallet> wrapper = selectUserWalletWrapper(params);
		wrapper.select("ifnull(sum(amount),0) as total");
		return Double.valueOf(getMap(wrapper).get("total").toString());
	}

	/**
	 * 统计用户累计金额
	 */
	public Double sumUserMoney(Long userId) {
		Map<String, Object> params = Kv.create().set("userId", userId)
			.set("type", 1).set("amountType", WalletAmountTypeEnum.MONEY.getType()
		);
		return sumByParams(params);
	}

	/**
	 * 统计用户累计积分
	 */
	public Double sumUserIntegral(Long userId) {
		Map<String, Object> params = Kv.create().set("userId", userId)
			.set("type", 1).set("amountType", WalletAmountTypeEnum.INTEGRAL.getType()
			);
		return sumByParams(params);
	}

}
